package com.qianguo.buyer.widget;


import android.content.Context;
import android.support.annotation.NonNull;

import com.eva.androidlibrary.utils.TimeUtil;
import com.qianguo.buyer.entity.DeliveryTime;
import com.qianguo.buyer.entity.KeyValuePair;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * 配送时间选择空间
 * Created by drakeet on 9/28/14.
 */
public class DeliveryTimeDialog extends ListContentDialog {
    public static final int TIME_DAY = 1;
    public static final int TIME_HOUR = 2;
    public static final int KEY_TODAY = 0;
    public static final int KEY_TOMORROW = 1;
    public static final int KEY_AFTER = 2;

    private List<String> mStringData = new ArrayList<>();
    private DeliveryTime mTimeData;

    public DeliveryTimeDialog(Builder builder) {
        super(builder);
        mTimeData = builder.mTime;
        if (builder.mTimeType == TIME_DAY) {
            mStringData.add(getDeliveryTodayTime());
            mStringData.add(getDeliveryTomorrowTime());
            mStringData.add(getDeliveryAfterTime());
        } else {
            mStringData = changeDeliveryTimeData(builder.mDayKey);
        }
        builder.mContentList = mStringData;

    }

    public static class Builder extends ListContentDialog.Builder {
        DeliveryTime mTime;
        int mTimeType;
        int mDayKey;
        TimeDataClickCallback mCallback;

        public Builder(@NonNull Context context) {
            super(context);
        }

        public Builder setDeliveryTimeData(@NonNull DeliveryTime time) {
            this.mTime = time;
            return this;
        }

        public Builder setTimeType(@NonNull int time) {
            this.mTimeType = time;
            return this;
        }

        public Builder setDayKey(@NonNull int time) {
            this.mDayKey = time;
            return this;
        }
        public Builder setTimeDataClickListener(@NonNull TimeDataClickCallback callback) {
            mCallback = callback;
            mListClickCallback = new ListCallback() {
                @Override
                public void onSelection(int position, String str) {
                    if(mDayKey==KEY_TODAY){
                        mCallback.onSelection(position, str, findTimeKeyByValue(mTime.getToday(),str));
                    }else if(mDayKey==KEY_TOMORROW){
                        mCallback.onSelection(position, str, findTimeKeyByValue(mTime.getTomorrow(),str));
                    }else{
                        mCallback.onSelection(position, str, findTimeKeyByValue(mTime.getAfter(),str));
                    }
                }
            };
            return this;
        }

        public DeliveryTimeDialog build() {
            return new DeliveryTimeDialog(this);
        }

        public DeliveryTimeDialog show() {
            DeliveryTimeDialog dialog = build();
            dialog.show();
            return dialog;
        }
    }

    public String getDeliveryTodayTime() {
        long currentTime = System.currentTimeMillis();
        Date messageDate = new Date(currentTime);
        String format;
        format = "yyyy/MM/dd " + TimeUtil.getWeekDayString(messageDate);

        return new SimpleDateFormat(format, Locale.CHINA).format(messageDate);
    }

    /**
     * 明天
     *
     * @return
     */
    public String getDeliveryTomorrowTime() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);
        date = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date) + " " + TimeUtil.getWeekDayString(date);
        return dateString;
    }

    /**
     * 後天
     *
     * @return
     */
    public String getDeliveryAfterTime() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 2);
        date = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date) + " " + TimeUtil.getWeekDayString(date);
        return dateString;
    }

    public List<String> changeDeliveryTimeData(int position) {
        List<String> temp;
        if (KEY_TODAY == position) {
            temp = getTimeMap(mTimeData.getToday());
        } else if (KEY_TOMORROW == position) {
            temp = getTimeMap(mTimeData.getTomorrow());
        } else {
            temp = getTimeMap(mTimeData.getAfter());
        }
        return temp;

    }

    public List<String> getTimeMap(Map<String, String> map) {
        List<String> temp = new ArrayList<>();
        for (String item : map.keySet()) {
            temp.add(map.get(item));
        }
        return temp;
    }

    public static String findTimeKeyByValue(Map<String, String> map,String val) {
        List<String> temp = new ArrayList<>();
        for (String item : map.keySet()) {
            temp.add(map.get(item));
            if(val.equals(map.get(item))){
                return item;
            }
        }
        return "";
    }
    public interface TimeDataClickCallback {
        void onSelection(int position,String str,String val);
    }


}


